package com.erp.myAdapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.LinearLayout
import com.erp.bean.CaiLiaoZhiLiangBean
import com.erp.R
import com.erp.util.Myutil
import com.erp.util.Sputil
import kotlinx.android.synthetic.main.lv_iv_item.view.*


/**
 * Created by Administrator on 2018/2/3/003.
 * 作者：刘洋
 */

class CaiLiaoPhotoKAdapter(private var mlist: List<CaiLiaoZhiLiangBean.photo>, private val mcontext: Context) : BaseAdapter() {


    private val minflater: LayoutInflater = LayoutInflater.from(mcontext)
    private var isadd = false
    internal var width = 0

    init {
        width = Sputil.sp("erp").getInt("width", width)
    }

    override fun getCount(): Int = mlist.size + 1

    override fun getItem(position: Int): Any = mlist[position]

    override fun getItemId(position: Int): Long = position.toLong()

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        var v :View
        v = minflater.inflate(R.layout.lv_iv_item, null)
        val iv = v.iv_item
        val params = iv.layoutParams as LinearLayout.LayoutParams
        params.height = width / 3
        iv.layoutParams = params
        if (position == mlist.size) {
            if (isadd) {
                iv.setImageResource(R.mipmap.erp_photo)
            } else {
                iv.visibility = View.GONE
            }
        } else {
            Myutil.showhttpphoto(mlist[position].uri, iv, mcontext)
        }
        return v
    }

    fun isadd(isadd: Boolean) {
        this.isadd = isadd
    }

    fun notifyA(list: List<CaiLiaoZhiLiangBean.photo>) {
        this.mlist = list
        notifyDataSetChanged()
    }
}
